package com.ketangpai.controller;


import com.ketangpai.DTO.AddCourseDTO;
import com.ketangpai.DTO.DeleteCourseDTO;
import com.ketangpai.entity.Course;
import com.ketangpai.entity.response.JSONResult;
import com.ketangpai.service.CourseService;
import com.ketangpai.util.RandomDataGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;
import java.util.UUID;
/**
 * @author msd
 * @date 2024/2/14 14:23
 */

@RestController
@CrossOrigin
@RequestMapping("/course")
//@Api
public class CourseController {


    @Autowired
    private CourseService courseService;

    @PostMapping(value = "/addCourse")
//    @ApiOperation(value = "添加课程",notes = "添加课程")
    public JSONResult addCourse(@RequestBody AddCourseDTO addCourseDTO) {
        //1.添加课程
        JSONResult jsonResult = new JSONResult<>();
        Course course = new Course();

        course.setId(UUID.randomUUID().toString().replace("-", ""));

//        course.setConditions(addCourseDTO.getConditions());

//        course.setClassName(addCourseDTO.getClassName());

        course.setCourseName(addCourseDTO.getCourseName());

        course.setManager(addCourseDTO.getManager());

//        Date currentTime = new Date();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String createTime = format.format(currentTime);
//        course.setCreateTime(createTime);

        //获取课程学期
        course.setSemester(addCourseDTO.getSemester());

        course.setYear(addCourseDTO.getYear());

        //获取背景图片地址
        course.setBGimg(addCourseDTO.getBGImgUrl());

        //随机分配课程码
        course.setAddCode(RandomDataGenerator.generateRandomData(6));

        boolean success = courseService.addCourse(course);
        if (success) {
            //2.根据id查找课程
            Course course2 = courseService.getCourseById(course.getId());
            courseService.SuccessAddCourse(course2);
            System.out.println(course2);
            jsonResult.setMessage("success");
            jsonResult.setData(course2);
        }
        else {
            jsonResult.setMessage("false");
        }
        return jsonResult;
    }

    /**
     * 展示所有课程
     * @return
     */
    @RequestMapping(value = "/showCourse",method = RequestMethod.GET)
//    @ApiOperation(value = "展示所有课程",notes = "展示所有课程")
    public JSONResult showCourse(@RequestParam("id")String id) {
        JSONResult jsonResult = new JSONResult<>();
        List<Course> courseList = courseService.showCourse(id);
        jsonResult.setData(courseList);
        jsonResult.setTotalCount(courseList.size());
        return jsonResult;
    }

    /**
     * 根据id获取课程
     * @param id
     * @return
     */
    @RequestMapping(value = "/getCourseById",method = RequestMethod.GET)
//    @ApiOperation(value = "根据id获取课程",notes = "根据id获取课程")
    public Course getCourseById(@RequestParam("id") String id) {
        Course course = courseService.getCourseById(id);
        return course;

    }

    /**
     * 删除课程
     * @param deleteCourseDTO
     * @return
     */
    @RequestMapping(value = "/deleteCourse",method = RequestMethod.POST)
//    @ApiOperation(value = "退课",notes = "退课")
    public JSONResult deleteCourse(@RequestBody DeleteCourseDTO deleteCourseDTO) {
        JSONResult jsonResult = new JSONResult<>();
        //1.判断这门课程是 “学” 还是 “教”
            //获取account
        Course course =new Course();
        AddCourseDTO addCourseDTO= new AddCourseDTO();
        boolean IsTeach = false;

        String courseId = String.valueOf(courseService.getCourseById(deleteCourseDTO.getCourseId()));
        course.setManager(addCourseDTO.getManager());


        IsTeach = courseService.getStatus(course.getManager(), courseId).equals("0");

        //2.“教”：删除选课表里面的选课，删除这门课程
        try{
            if (IsTeach){
                courseService.delCourse(courseId,course.getManager());

            }else {
                //3."学"：删除选课表里面这条信息
                courseService.dropStudent(course.getManager(),courseId);
            }

            jsonResult.setMessage("success");
            jsonResult.setData(course);
        }catch (Exception e){
            System.out.println(e);
        }
        return jsonResult;
    }
//
//    /**
//     * 编辑课程
//     * @param editCourseDTO
//     * @return
//     */
//    @RequestMapping(value = "/editCourse",method=RequestMethod.POST)
////    @ApiOperation(value = "编辑课程",notes = "编辑课程")
//    public JSONResult editCourse(@RequestBody EditCourseDTO editCourseDTO) {
//        JSONResult jsonResult = new JSONResult<>();
//        boolean success = courseService.editCourse(editCourseDTO);
//        if(success) {
//            jsonResult.setMessage("success");
//        }
//        else {
//            jsonResult.setMessage("false");
//        }
//        return jsonResult;
//    }


    @RequestMapping(value = "/getAllCourse",method = RequestMethod.GET)
//    @ApiOperation(value = "获取教师教授的课程、选择的课程",notes = "获取教师教授的课程、选择的课程")
    public JSONResult getAllCourse(@RequestParam("teacherId")String teacherId) {
        JSONResult jsonResult = new JSONResult<>();
        List<Course> courseLsList = courseService.getAllCourse(teacherId);
        jsonResult.setData(courseLsList);
        return jsonResult;
    }
    /**
     * 获取课程成员
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/getCourseMember",method = RequestMethod.GET)
//    @ApiOperation(value = "获取课程成员",notes = "获取课程成员")
    public List<String> getCourseMember(@RequestParam("courseId")String courseId){
        List<String> stuMember = courseService.getStuMember(courseId);
        List<String> teaMember = courseService.getTeaMember(courseId);
        stuMember.addAll(teaMember);
        return stuMember;
    }

    @RequestMapping(value = "/getBGimg",method = RequestMethod.GET)
//    @ApiOperation(value = "获取课程背景图片",notes = "获取课程背景图片")
    public Image getBGimg(@RequestParam("courseId")String courseId){
        Image BGimg = courseService.getBGimg(courseId);
        return BGimg;
    }
}
